package com.yxh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.yxh.dto.LessonDateDto;
import com.yxh.dto.LessonDto;
import com.yxh.dto.LessonItemDto;
import com.yxh.dto.UserDto;
import com.yxh.util.Constant;
import com.yxh.util.HttpUtils;

public class DataService {

	public static final String DATA_SUCCESS = "1";
	/*** 获取应用初始化信息 ***/
	public static UserDto getAPPInfo(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				dto = new UserDto();
				dto.result = jo.getString("status");
				if (DATA_SUCCESS.equals(dto.result)) {
					JSONObject data = jo.optJSONObject("arrUserinfo");
					if (data != null) {
						dto.uid = data.optString("uid");
						dto.nickname = data.optString("username");
						dto.userSex = data.optString("sex");// "sex":"m"男 "f"女
						dto.phoneNumber = data.optString("mobileNo");
						dto.userPic = data.optString("userIcon");
						dto.province = data.optString("province");
						dto.city = data.optString("city");
						dto.area = data.optString("area");
						dto.userQrCode = jo.optString("user2dcode");
						dto.versions = jo.optString("versions");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 获取首页默认课程 ***/
	public static LessonDto getLessonList(Task task) {
		LessonDto dto = null;
		try {
			// info:{"status":"1","errcode":"","errmsg":"","arrLessoninfo":{"name":"\u8bfe\u7a0b\u540d\u79f0","lessonId":"\u8bfe\u7a0b\u7f16\u7801"},"arrData":[{"type":"1","title":"\u6807\u9898\u6807\u9898","intro":"\u7b80\u4ecb","url":"http:\/\/www.baidu.com"},{"type":"1","title":"\u6807\u9898\u6807\u9898","intro":"1.\u7b80\u4ecb\n2.\u7b80\u4ecb\n3.\u7b80\u4ecb","url":"http:\/\/www.baidu.com"}]}
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				dto = new LessonDto();
				dto.result = jo.optString("status");
				dto.message = jo.optString("errmsg");
				dto.pos = (String) (task.getTaskParam().get("pos"));
				if (DATA_SUCCESS.equals(dto.result)) {
					JSONObject data = jo.optJSONObject("arrLessoninfo");
					if (data != null) {
						dto.name = data.optString("name");
						dto.lessonId = data.optString("lessonId");
						dto.intro = data.optString("intro");
						dto.url = data.optString("icon");
						if (!TextUtils.isEmpty(data.optString("percent")))
							dto.percent = Integer.parseInt(data
									.optString("percent"));

						JSONArray ja2 = data.optJSONArray("arrNode");
						if (ja2 != null) {
							LessonDateDto item;
							for (int j = 0; j < ja2.length(); j++) {
								JSONObject jo2 = ja2.getJSONObject(j);
								item = new LessonDateDto();
								item.nodeName = jo2.optString("node_name");
								item.nodeDate = jo2.optString("node_date");
								dto.dateList.add(item);
							}
						}

						JSONArray ja = jo.optJSONArray("arrData");
						if (ja != null) {
							LessonItemDto item;
							for (int i = 0; i < ja.length(); i++) {
								JSONObject itemJo = ja.getJSONObject(i);
								item = new LessonItemDto();
								item.type = itemJo.optString("type");
								item.title = itemJo.optString("title");
								item.intro = itemJo.optString("intro");
								item.className = itemJo.optString("class");
								item.topStory = itemJo.optString("topStory");
								item.url = itemJo.optString("url");
								dto.list.add(item);
							}
						}
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 获取我的课程 ***/
	public static List<LessonDto> getMyLessonList(Task task) {
		List<LessonDto> list = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				list = new ArrayList<LessonDto>();
				String result = jo.optString("status");
				LessonDto item;
				if (DATA_SUCCESS.equals(result)) {
					JSONArray ja1 = jo.optJSONArray("arrLesson");
					if (ja1 != null) {
						for (int i = 0; i < ja1.length(); i++) {
							JSONObject data = ja1.getJSONObject(i);
							item = new LessonDto();
							item.lessonId = data.optString("lessonId");
							item.name = data.optString("title");
							item.intro = data.optString("intro");
							item.url = data.optString("icon");
							item.isDefault = data.optString("default");// 0:不是默认////
																		// 1:默认
							item.state = "0";
							item.result = result;
							JSONArray nodeJa = data.optJSONArray("arrNode");
							LessonDateDto dateItem;
							if (nodeJa != null) {
								for (int j = 0; j < nodeJa.length(); j++) {
									JSONObject dateJo = nodeJa.getJSONObject(j);
									dateItem = new LessonDateDto();
									dateItem.nodeName = dateJo
											.optString("node_name");
									dateItem.nodeDate = dateJo
											.optString("node_date");
									item.dateList.add(dateItem);
								}
							}
							list.add(item);
						}
					}

					JSONArray ja2 = jo.optJSONArray("arrFinLesson");
					if (ja2 != null) {
						for (int i = 0; i < ja2.length(); i++) {
							JSONObject data = ja2.getJSONObject(i);
							item = new LessonDto();
							item.lessonId = data.optString("lessonId");
							item.name = data.optString("title");
							item.intro = data.optString("intro");
							item.url = data.optString("icon");
							item.isDefault = data.optString("default");// 0:不是默认//
																		// 1:默认
							item.state = "1";
							item.result = result;
							JSONArray nodeJa = data.optJSONArray("arrNode");
							LessonDateDto dateItem;
							if (nodeJa != null) {
								for (int j = 0; j < nodeJa.length(); j++) {
									JSONObject dateJo = nodeJa.getJSONObject(j);
									dateItem = new LessonDateDto();
									dateItem.nodeName = dateJo
											.optString("node_name");
									dateItem.nodeDate = dateJo
											.optString("node_date");
									item.dateList.add(dateItem);
								}
							}
							list.add(item);
						}
					}

					JSONArray ja3 = jo.optJSONArray("arrCancelLesson");
					if (ja3 != null) {
						for (int i = 0; i < ja3.length(); i++) {
							JSONObject data = ja3.getJSONObject(i);
							item = new LessonDto();
							item.lessonId = data.optString("lessonId");
							item.name = data.optString("title");
							item.intro = data.optString("intro");
							item.url = data.optString("icon");
							item.isDefault = data.optString("default");// 0:不是默认//
																		// 1:默认
							item.state = "2";
							item.result = result;
							JSONArray nodeJa = data.optJSONArray("arrNode");
							LessonDateDto dateItem;
							if (nodeJa != null) {
								for (int j = 0; j < nodeJa.length(); j++) {
									JSONObject dateJo = nodeJa.getJSONObject(j);
									dateItem = new LessonDateDto();
									dateItem.nodeName = dateJo
											.optString("node_name");
									dateItem.nodeDate = dateJo
											.optString("node_date");
									item.dateList.add(dateItem);
								}
							}
							list.add(item);
						}
					}
				} else {
					item = new LessonDto();
					item.result = result;
					item.message = jo.optString("errmsg");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/*** 修改个人资料 ***/
	public static UserDto edit_userinfo(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.optString("status");
			dto.message = jo.optString("errmsg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 意见反馈 ***/
	public static UserDto sendFeedback(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.optString("status");
			dto.message = jo.optString("errmsg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 取消课程 ***/
	public static UserDto cancelLesson(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.optString("status");
			dto.message = jo.optString("errmsg");
			dto.uid = (String) (task.getTaskParam().get("lesson_id"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 用户登录 ***/
	public static UserDto login(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				dto = new UserDto();
				dto.result = jo.optString("status");
				if (DATA_SUCCESS.equals(dto.result)) {
					JSONObject data = jo.optJSONObject("arrUserinfo");
					if (data != null) {
						dto.uid = data.optString("uid");
						dto.nickname = data.optString("username");
						dto.userSex = data.optString("sex");// "sex":"m"男 "f"女
						dto.phoneNumber = data.optString("mobileNo");
						dto.userPic = data.optString("userIcon");
						dto.province = data.optString("province");
						dto.city = data.optString("city");
						dto.area = data.optString("area");
						dto.userQrCode = jo.optString("user2dcode");
						dto.token = jo.optString("token");
					}
				}
				dto.message = jo.optString("errmsg");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 用户注册(发送手机号 获取验证码) ***/
	public static UserDto getCode(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.optString("status");
			dto.message = jo.optString("errmsg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 添加用户头像 ***/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static UserDto upLoadPicInfo(Task task) {
		UserDto dto = null;
		Map map = task.getTaskParam();
		if (map == null) {
			return null;
		}
		Map<String, String> contents = new HashMap<String, String>();
		Map<String, String> paths = new HashMap<String, String>();
		Iterator<String> iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			if ("upFile".equals(key)) {

				String value = (String) map.get(key);
				paths.put("img", value);
			} else {
				contents.put(key, (String) map.get(key));
			}
		}
		String info = HttpUtils.upLoadFormFile(Constant.APP_PIC_URL, paths,
				contents);
		if (info != null) {
			JSONObject jo;
			try {
				jo = new JSONObject(info);
				dto = new UserDto();
				dto.result = jo.getString("status");
				dto.message = jo.optString("errmsg");
				dto.userPic = jo.optString("icon");
//				if ("1".equals(flag)) {
//					
//					// if ("none".equals(jo.optString("imagePath"))) {
//					// dto.userPic = "";
//					// } else if (jo.optString("imagePath").indexOf("http") !=
//					// -1) {
//					// dto.userPic = jo.optString("imagePath");
//					// } else {
//					// dto.userPic = Constant.BSER_URL_PIC +
//					// jo.optString("imagePath");
//					// }
//
//				} else if ("0".equals(flag)) {
//					dto.result = flag;
//					dto.message = jo.optString("message");
//
//				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return dto;
	}

	/*** 输入课程码拉取课程列表 ***/
	public static List<LessonDto> getLessonPacks(Task task) {
		List<LessonDto> list = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				list = new ArrayList<LessonDto>();
				JSONObject jo = new JSONObject(info);
				String result = jo.optString("status");
				String message = jo.optString("errmsg");
				LessonDto lDto;
				if (DATA_SUCCESS.equals(result)) {
					JSONArray ja = jo.optJSONArray("arrLesson");
					if (ja != null) {
						JSONObject jo2;
						for (int i = 0; i < ja.length(); i++) {
							lDto = new LessonDto();
							jo2 = ja.getJSONObject(i);
							lDto.name = jo2.optString("title");
							lDto.intro = jo2.optString("intro");
							lDto.lessonId = jo2.optString("lessonId");
							lDto.url = jo2.optString("icon");
							lDto.isSelected = "0"; // 默认为未选中状态
							lDto.result = result;
							JSONArray ja2 = jo2.optJSONArray("arrNode");
							LessonDateDto item;
							if (ja != null) {
								for (int j = 0; j < ja2.length(); j++) {
									JSONObject data = ja2.getJSONObject(j);
									item = new LessonDateDto();
									item.nodeName = data.optString("node_name");
									item.nodeDate = data.optString("node_date");
									lDto.dateList.add(item);
								}
							}
							list.add(lDto);
						}
					}

				} else {
					lDto = new LessonDto();
					lDto.result = result;
					lDto.message = message;
					list.add(lDto);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/*** 添加课程 ***/
	public static List<LessonDto> addLessonForInput(Task task) {
		List<LessonDto> list = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				list = new ArrayList<LessonDto>();
				JSONObject jo = new JSONObject(info);
				String result = jo.optString("status");
				String message = jo.optString("errmsg");
				LessonDto lDto;
				if (DATA_SUCCESS.equals(result)) {
					JSONArray ja = jo.optJSONArray("arrLesson");
					if (ja != null) {
						JSONObject jo2;
						for (int i = 0; i < ja.length(); i++) {
							lDto = new LessonDto();
							jo2 = ja.getJSONObject(i);
							lDto.lessonId = jo2.optString("lessonId");
							lDto.name = jo2.optString("title");
							lDto.intro = jo2.optString("intro");
							lDto.url = jo2.optString("icon");
							lDto.isDefault = jo2.optString("default");
							lDto.result = result;
							JSONArray ja2 = jo2.optJSONArray("arrNode");
							LessonDateDto item;
							if (ja != null) {
								for (int j = 0; j < ja2.length(); j++) {
									JSONObject data = ja2.getJSONObject(j);
									item = new LessonDateDto();
									item.nodeName = data.optString("node_name");
									item.nodeDate = data.optString("node_date");
									lDto.dateList.add(item);
								}
							}
							list.add(lDto);
						}
					}

				} else {
					lDto = new LessonDto();
					lDto.result = result;
					lDto.message = message;
					list.add(lDto);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/*** 二维码扫描获取课程id ***/
	public static UserDto addLessonForCode(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				dto = new UserDto();
				dto.result = jo.optString("status");
				dto.message = jo.optString("errmsg");
				dto.userType = jo.optString("type");
				if (DATA_SUCCESS.equals(dto.result)) {
					JSONObject ja = jo.optJSONObject("arrData");
					if (ja != null) {
						if ("1".equals(dto.userType)) { // 医生
							dto.uid = ja.optString("docId");
						} else if ("2".equals(dto.userType)) { // 用户

						} else if ("3".equals(dto.userType)) { // 课程
							dto.uid = ja.optString("lessonId");
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 设置课程时间节点 ***/
	public static UserDto setClassNode(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.optString("status");
			dto.message = jo.optString("errmsg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

}